package com.mindao.app.site.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.mindao.app.common.spring.model.BaseModel;
import com.mindao.app.common.spring.page.Pagination;
import com.mindao.app.common.spring.service.BaseService;
import com.mindao.app.site.exception.EnrollException;
import com.mindao.app.site.model.Event;
import com.mindao.app.site.model.EventEnroll;
import com.mindao.app.site.service.EventService;
import com.mindao.utils.config.EaCommonUtils;

/**
/* 版权所有： 广州敏道科技有限公司
/*
/* 功能描述：
/*
/* 创 建 人：李国才
/* 创建时间：2016年6月16日 下午9:27:14 	
 **/
@Component ("eventService")
public class EventServiceImpl implements EventService {
	private final Logger log = LoggerFactory.getLogger(EventServiceImpl.class);
	
	@javax.annotation.Resource(name = "baseService")
	private BaseService baseService;
	
	@Override
	public EventEnroll getEventEnroll(Long eventId,Long currentUserId, String openid) {
		// TODO Auto-generated method stub
		if (currentUserId!=null){
			List<EventEnroll> list=baseService.find("from EventEnroll t where t.event.id=? and t.userId=? and t.disabled=0",new Object[]{eventId,currentUserId});
			if (list.size()>0){
				return list.get(0);
			}
		}
		if (openid!=null){
			List<EventEnroll> list=baseService.find("from EventEnroll t where t.event.id=? and t.openid=? and t.disabled=0",new Object[]{eventId,openid});
			if (list.size()>0){
				return list.get(0);
			}
		}
		return null;
	}

	@Override
	public Pagination getEventEnrolls(Long eventId, Pagination page) {
		// TODO Auto-generated method stub
		 Map<String, Object> params=new HashMap<>();
		params.put("eventId", eventId); 
		try {
			baseService.findByHQLPage("from EventEnroll t where t.event.id=:id and t.disabled=0", params, page);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error(e.getMessage(),e);
		}
		return page;
	}
	
	/**
	 * 判断是否允许报名
	 * @param event
	 * @return
	 */
	@Override
	public boolean getEnrollable(Event event){
		if (event.getDeadline().getTime()>System.currentTimeMillis()){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public Event getCurrentEvent() {
		// TODO Auto-generated method stub
		Object obj=new Date();
		List<Event> list=baseService.find("from Event t where t.deadline>? and t.status>? order by t.id desc",new Object[]{obj,BaseModel.STATUS_REMOVED});
		if (list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@Override
   public EventEnroll createEventEnroll(Event event,String enrollUuid,Long currentUserId,String openid,String nickName,String userName,String qq) throws EnrollException{
		// TODO Auto-generated method stub
		log.info("555555555555555");
		if (! getEnrollable(event)){
			throw new EnrollException("报名时间已结束");
 
		}
		EventEnroll enroll=null;
		log.info("66666666666666666666");
		if (!StringUtils.isBlank(enrollUuid)){
			enroll=(EventEnroll)(EventEnroll)EaCommonUtils.getObjectByUuid(enrollUuid, "EventEnroll");
		}else{
			enroll=new EventEnroll();
		}
		enroll.setName(userName);
		enroll.setQq(qq);
		log.info("7777777777777777777777777777777");
 
		enroll.setEvent(event); 
		enroll.setDisabled(EventEnroll.DISABLED_FALSE);
		enroll.setUserId(currentUserId);
		
		enroll.setOpenid(openid);
		enroll.setNickname(nickName);
		if (!StringUtils.isBlank(enrollUuid)){
			baseService.update(enroll);
		}else{
			baseService.create(enroll);
		}
		log.info("888888888888888888888888888");
		return enroll;
	}

}
